Imports Microsoft.VisualBasic
Imports System.Collections.Generic
Imports System.Data

Namespace sk.DAL
    Public MustInherit Class ProductProvider
        Inherits DataAccess
        Sub New()
            Me.ConnectionString = Globals.Settings.Amazon.ConnectionString
        End Sub
        Private Shared _instance As ProductProvider = Nothing
        Public Shared ReadOnly Property Instance() As ProductProvider
            Get
                If IsNothing(_instance) Then
                    _instance = CType(Activator.CreateInstance(Type.GetType(Globals.Settings.Amazon.ProviderType)), ProductProvider)
                End If

                Return _instance
            End Get
        End Property

        Public MustOverride Function GetAllProducts() As Generic.List(Of Product)
        Public MustOverride Function GetAllProductsByCategory() As Generic.List(Of Product)
        Public MustOverride Function CreateProduct(ByVal Product As Product) As Boolean
        Public MustOverride Function DeleteProduct(ByVal ASIN As String) As Boolean
        Public MustOverride Function UpdateProduct(ByVal Product As Product) As Boolean
        Public MustOverride Function GetProductByID(ByVal ASIN As String) As Product
        Public MustOverride Function GetNextID() As Integer

        Protected Overridable Function GetProductFromReader(ByVal reader As IDataReader) As Product

            Dim prod As New Product
            With prod
                If IsDBNull(reader("ASIN")) Then .ASIN = 0 Else .ASIN = reader("ASIN").ToString
                If IsDBNull(reader("Title")) Then .Title = "" Else .Title = reader("Title").ToString
                If IsDBNull(reader("Description")) Then .Description = "" Else .Description = reader("Description").ToString
                If IsDBNull(reader("CategoryID")) Then .CategoryID = 0 Else .CategoryID = CInt(reader("CategoryID"))
            End With
            Return prod
        End Function

        Protected Overridable Function GetProductCollectionFromReader( _
                ByVal reader As IDataReader) _
                As List(Of Product)
            Dim ProductList As New List(Of Product)
            Try
                While (reader.Read)
                    ProductList.Add(GetProductFromReader(reader))
                End While
            Catch ex As Exception
                Throw New Exception("from GetProductCollectionFromReader", ex)
            End Try
            Return ProductList
        End Function

    End Class
End Namespace


